package com.hz.web.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.hz.web.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hz.web.service.ImanyDemoService;

/**
 * 测试多数据源
 *
 * @author Administrator
 */
@Controller
@RequestMapping("many")
public class ManyDemoContorller {

    @Resource
    private ImanyDemoService manyDemoService;


    /**
     * 查询自然人基本信息
     *
     * @param
     * @return
     */
    @RequestMapping("queryGrMsgByPersonalId")
    @ResponseBody
    public Map<String, Object> queryGrMsgByPersonalId(String id, String indexType) {
        if (id == null || "".equals(id)
                || indexType == null || "".equals(indexType)) {
            return null;
        }
        String personalid = id;
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> parmsMap = new HashMap<String, Object>();
        parmsMap.put("fieldName", "INDEX_NAME,INDEX_VIEW");
        parmsMap.put("tableName", "SYS_INDEX");
        parmsMap.put("indexType", indexType);
        parmsMap.put("kfdj", "A");//开放等级
        parmsMap.put("sx", "ORDER BY SX");
        List<Map<String, Object>> list = manyDemoService.queryGrMsgByPersonalId(parmsMap);
        int count = 0;
        for (Map<String, Object> map : list) {
            String viewStr = (String) map.get("INDEX_VIEW");//表名
            parmsMap.clear();
            parmsMap.put("fieldName", "*");
            parmsMap.put("tableName", viewStr);
            parmsMap.put("personalid", personalid);
            parmsMap.put("indextype", indexType);
            List<Map<String, Object>> numls = manyDemoService.queryGrMsgByPersonalId(parmsMap);
            if (numls.size() == 0) {
                continue;
            }
            String resultNum = "";
            for (Map<String, Object> map2 : numls) {
                resultNum = String.valueOf(map2.get("RESULT"));
            }
            if ("".equals(resultNum.trim()) || "0".equals(resultNum.trim())
                    || "null".equals(resultNum.trim())
                    ) {
                continue;
            }
            parmsMap.clear();
            parmsMap.put("fieldName", "INDEX_NAME,INDEX_SQL");
            parmsMap.put("tableName", "sys_index");
            parmsMap.put("vgrindex", viewStr);
            parmsMap.put("tableId", "notnull");
            List<Map<String, Object>> sqlStr = manyDemoService.queryGrMsgByPersonalId(parmsMap);
            if (sqlStr.size() == 0) {
                continue;
            }
            String sql = "";
            String groupName = "";//组名
            for (Map<String, Object> map2 : sqlStr) {
                sql = (String) map2.get("INDEX_SQL");
                groupName = (String) map2.get("INDEX_NAME");
            }
            if ("".equals(sql.trim())) {
                continue;
            }
            parmsMap.clear();
            parmsMap.put("fieldName", "ename");
            parmsMap.put("tableName", "sys_table");
            List<Map<String, Object>> allTable = manyDemoService.queryGrMsgByPersonalId(parmsMap);
            if (allTable.size() == 0) {
                continue;
            }
            String resultTable = "";
            for (Map<String, Object> map2 : allTable) {
                String ename = (String) map2.get("ENAME");
                boolean istrue = sql.contains(ename);
                if (istrue) {
                    resultTable = ename;
                    break;
                }
            }
            if ("".equals(resultTable.trim())) {
                continue;
            }
            List<FieldEntity> resultField = new ArrayList<FieldEntity>();
            parmsMap.clear();
            parmsMap.put("resultTable", resultTable);
            resultField = manyDemoService.queryFieldByTable(parmsMap);
            String fieldStr = "";
            if (resultField.size() == 0) {
                continue;
            }
            List<String> fieldls = new ArrayList<String>();

            for (FieldEntity fieldEntity : resultField) {
                fieldStr += fieldEntity.getEname() + ",";//列名
                fieldls.add(fieldEntity.getEname() + "," + fieldEntity.getCname());

            }
            fieldStr = fieldStr.substring(0, fieldStr.length() - 1);
            parmsMap.clear();
            parmsMap.put("fieldName", fieldStr);
            parmsMap.put("tableName", resultTable);
            parmsMap.put("personalid", personalid);
            parmsMap.put("indextype", indexType);
            List<Map<String, Object>> resultList = manyDemoService.queryGrMsgByPersonalId(parmsMap);

            count++;
            List<String> cnamels = new ArrayList<String>();//列集合
            List<List<String>> listval = new ArrayList<List<String>>();//列对应的数据集合
            List<String> enamevalls = new ArrayList<String>();
            int cnamelsCount = 0;
            for (Map<String, Object> map2 : resultList) {
                cnamelsCount++;
                enamevalls = new ArrayList<String>();
                for (String string : fieldls) {
                    String[] field = string.split(",");
                    String f = field[0];
                    if (f == null || "".equals(f.trim())) {
                        continue;
                    }
                    if (map2.get(f) == null) {
                        continue;
                    }
                    if (cnamelsCount == 1) {
                        cnamels.add(field[1]);//存入列名 ，列名只存入一次
                    }
                    String enameval = String.valueOf(map2.get(f));
                    if (field[1].contains("状态")) {
                        if ("1".equals(enameval)) {
                            enameval = "正常";
                        }
                    }
                    enamevalls.add(enameval);
                }
                listval.add(enamevalls);
            }

            result.put("groupName" + count, groupName);//组名
            result.put("cnamels" + count, cnamels);//列名
            result.put("listval" + count, listval);//数据
        }
        //自然人
        if ("1".equals(indexType)) {
            parmsMap.clear();
            parmsMap.put("fieldName", "V_GR_INDEX_2072,V_GR_INDEX_2073");
            parmsMap.put("tableName", "GH_PERSONAL_TABLE");
            parmsMap.put("personalid", personalid);
            parmsMap.put("indextype", indexType);
            List<Map<String, Object>> people = manyDemoService.queryGrMsgByPersonalId(parmsMap);
            result.put("people", people);//自然人姓名和身份证号
        }
        result.put("count", count);
        return result;
    }


    /**
     * 查询企业基本信息
     *
     * @param
     * @return
     */
    @RequestMapping("queryQyMsgByEnterpriseId")
    @ResponseBody
    public Map<String, Object> queryQyMsgByEnterpriseId(String enterpriseid) {
        if (enterpriseid == null || "".equals(enterpriseid)) {
            return null;
        }
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> parmsMap = new HashMap<String, Object>();
        parmsMap.put("enterpriseId", enterpriseid);
        List<GhEnterpriseTableEntity> list = manyDemoService.queryQyMsgByEnterpriseId(parmsMap);
        result.put("rows", list);
        return result;
    }



    /**
     * 查询企业基本信息
     *
     * @param
     * @return
     */
    @RequestMapping(value = "queryQyMsg", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> queryQyMsgBy(String enterpriseid) {
        if (enterpriseid == null || "".equals(enterpriseid)) {
            return null;
        }
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> parmsMap = new HashMap<String, Object>();
        parmsMap.put("enterpriseId", enterpriseid);
        List<GhEnterpriseTableEntity> list = manyDemoService.queryQyMsgByEnterpriseId(parmsMap);
        List newList = new ArrayList<>();

        for (GhEnterpriseTableEntity ghEnterpriseTableEntity : list) {
            newList.add(new Objectentity("法人Id", ghEnterpriseTableEntity.getENTERPRISE_ID()));
            newList.add(new Objectentity("企业名称", ghEnterpriseTableEntity.getV_QY_INDEX_2060()));
            newList.add(new Objectentity("统一社会信用代码", ghEnterpriseTableEntity.getV_QY_INDEX_2061()));
            newList.add(new Objectentity("类别", ghEnterpriseTableEntity.getV_QY_INDEX_2062()));
            newList.add(new Objectentity("企业（机构）类型", ghEnterpriseTableEntity.getV_QY_INDEX_2063()));
            newList.add(new Objectentity("注册资本（万元）", ghEnterpriseTableEntity.getV_QY_INDEX_2064()));
            newList.add(new Objectentity("注册资本（金）币种", ghEnterpriseTableEntity.getV_QY_INDEX_2065()));
            newList.add(new Objectentity("行业门类", ghEnterpriseTableEntity.getV_QY_INDEX_2066()));
            newList.add(new Objectentity("成立日期", ghEnterpriseTableEntity.getV_QY_INDEX_2067()));
            newList.add(new Objectentity("登记机关", ghEnterpriseTableEntity.getV_QY_INDEX_2068()));
            newList.add(new Objectentity("地址", ghEnterpriseTableEntity.getV_QY_INDEX_2069()));
            newList.add(new Objectentity("状态", ghEnterpriseTableEntity.getV_QY_INDEX_2070()));
            newList.add(new Objectentity("经营范围", ghEnterpriseTableEntity.getV_QY_INDEX_2071()));
        }

        result.put("rows", list);
        result.put("resultCode", "0");
        result.put("resultMsg", "查询成功");
        return result;
    }


    @RequestMapping("queryCount")
    @ResponseBody
    public Map<String, Object> queryCount(HttpServletResponse response) {
        //response.addHeader("Access-Control-Allow-Origin","http://192.168.4.172");

        Map<String, Object> result = new HashMap<String, Object>();
        int row = manyDemoService.queryCount();
        result.put("rows", row);
        return result;
    }

    @RequestMapping("queryCollection")
    @ResponseBody
    public Map<String, Object> queryCollection(HttpServletResponse response, int page) {
        //response.addHeader("Access-Control-Allow-Origin","http://192.168.4.172");

        Map<String, Object> result = new HashMap<String, Object>();
        List<CollectionVo> list = manyDemoService.queryCollection(page);

        result.put("list", list);
        return result;
    }

    @RequestMapping("querySgs")
    @ResponseBody
    public Map<String, Object> querySgsCF(HttpServletResponse response, Integer page, Integer count, String ckType, String ouid, String mc) {
        //response.addHeader("Access-Control-Allow-Origin","http://192.168.4.172");

        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> parmsMap = new HashMap<String, Object>();
        if (null == count || 0 == count) {
            count = 10;
        }
        if (page != null && count != null && count != 0) {
            int start = 0;
            int end = 0;
            start = (page - 1) * count;
            end = start + count;
            if (start > 1) {
                start = start + 1;
            }
            parmsMap.put("start", start);
            parmsMap.put("end", end);
        }
        if (ouid != null && !"".equals(ouid) && !"0".equals(ouid)) {
            parmsMap.put("ouid", ouid);
        }

        if (mc != null && !"".equals(mc) && !"0".equals(mc)) {
            try {
                mc = URLDecoder.decode(URLDecoder.decode(mc, "utf-8"), "utf-8");
                parmsMap.put("mc", "%" + mc + "%");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }


        if (ckType.equals("1")) {
            List<XzxkEntity> list = manyDemoService.queryXZXK(parmsMap);
            List<XzxkEntity> listbm = manyDemoService.queryXKBM();
            int total = manyDemoService.queryXZXKCount(parmsMap);
            int pageCount = (total % count == 0 ? (total / count) : (total / count) + 1);
            result.put("pageCount", pageCount);
            result.put("list", list);
            result.put("listbm", listbm);
            result.put("total", total);
        } else if (ckType.equals("2")) {
            List<XzcfEntity> list = manyDemoService.queryXZCF(parmsMap);
            List<XzcfEntity> listbm = manyDemoService.queryCFBM();
            int total = manyDemoService.queryXZCFCount(parmsMap);
            int pageCount = (total % count == 0 ? (total / count) : (total / count) + 1);
            result.put("pageCount", pageCount);
            result.put("list", list);
            result.put("listbm", listbm);
            result.put("total", total);
        }
        result.put("resultCode", "0");
        result.put("resultMsg", "查询成功");

        return result;
    }


    @RequestMapping("querySgsxx")
    @ResponseBody
    public Map<String, Object> querySgsxx(HttpServletResponse response, String ckType, String sid) {
        //response.addHeader("Access-Control-Allow-Origin","http://192.168.4.172");

        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> parmsMap = new HashMap<String, Object>();
        if (sid != null && sid != "") {
            parmsMap.put("sid", sid);
        }

        if (ckType.equals("1")) {
            List<XzxkEntity> list = manyDemoService.queryXKxx(parmsMap);
            result.put("list", list);
        } else if (ckType.equals("2")) {
            List<XzcfEntity> list = manyDemoService.queryCFxx(parmsMap);
            result.put("list", list);
        }
        return result;
    }

    @RequestMapping("queryAppSgsxx")
    @ResponseBody
    public Map<String, Object> queryAppSgsxx(HttpServletResponse response, String ckType, String sid) {
        //response.addHeader("Access-Control-Allow-Origin","http://192.168.4.172");

        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> parmsMap = new HashMap<String, Object>();
        if (sid != null && sid != "") {
            parmsMap.put("sid", sid);
        }

        if (ckType.equals("1")) {
            List<XzxkEntity> list = manyDemoService.queryXKxx(parmsMap);
            List newList = new ArrayList<>();
            for (XzxkEntity xzxkEntity : list) {
                newList.add(new Objectentity("决定书文号", xzxkEntity.getWsh()));
                newList.add(new Objectentity("项目名称", xzxkEntity.getMc()));
                newList.add(new Objectentity("审批类别", xzxkEntity.getSplb()));
                newList.add(new Objectentity("行政相对人名称", xzxkEntity.getXdr()));
                newList.add(new Objectentity("法定代表人姓名", xzxkEntity.getFr()));
                newList.add(new Objectentity("社会信用代码", xzxkEntity.getXdrshxym()));
                newList.add(new Objectentity("组织机构代码", xzxkEntity.getXdrzdm()));
                newList.add(new Objectentity("工商登记码", xzxkEntity.getXdrgsdj()));
                newList.add(new Objectentity("税务登记号", xzxkEntity.getXdrswdj()));
                newList.add(new Objectentity("身份证号", xzxkEntity.getXdrsfz()));
                newList.add(new Objectentity("许可机构", xzxkEntity.getXzjg()));
                newList.add(new Objectentity("许可决定日期", xzxkEntity.getJdrq()));
                newList.add(new Objectentity("许可截止期", xzxkEntity.getJzq()));
                newList.add(new Objectentity("地方编码", xzxkEntity.getDfbm()));
                newList.add(new Objectentity("状态", xzxkEntity.getZt()));
                newList.add(new Objectentity("备注", xzxkEntity.getBz()));
                newList.add(new Objectentity("许可内容", xzxkEntity.getNr()));
            }

            result.put("list", newList);
        } else if (ckType.equals("2")) {
            List<XzcfEntity> list = manyDemoService.queryCFxx(parmsMap);
            List newList = new ArrayList<>();
            for (XzcfEntity xzcfEntity : list) {
                Map map = new HashMap();
                newList.add(new Objectentity("决定书文号", xzcfEntity.getWsh()));
                newList.add(new Objectentity("处罚名称", xzcfEntity.getMc()));
                newList.add(new Objectentity("处罚类别1", xzcfEntity.getCflb1()));
                newList.add(new Objectentity("处罚类别2", xzcfEntity.getCflb2()));
                newList.add(new Objectentity("行政相对人名称", xzcfEntity.getXdrmc()));
                newList.add(new Objectentity("法定代表人姓名", xzcfEntity.getFr()));
                newList.add(new Objectentity("社会信用代码", xzcfEntity.getXdrshxym()));
                newList.add(new Objectentity("组织机构代码", xzcfEntity.getXdrzdm()));
                newList.add(new Objectentity("工商登记码", xzcfEntity.getXdrgsdj()));
                newList.add(new Objectentity("税务登记号", xzcfEntity.getXdrswdj()));
                newList.add(new Objectentity("身份证号", xzcfEntity.getXdrsfz()));
                newList.add(new Objectentity("处罚机构", xzcfEntity.getXzjg()));
                newList.add(new Objectentity("决定日期", xzcfEntity.getJdrq()));
                newList.add(new Objectentity("地方编码", xzcfEntity.getDfbm()));
                newList.add(new Objectentity("状态", xzcfEntity.getZt()));
                newList.add(new Objectentity("备注", xzcfEntity.getBz()));
                newList.add(new Objectentity("处罚事由", xzcfEntity.getSy()));
                newList.add(new Objectentity("处罚依据", xzcfEntity.getYj()));
                newList.add(new Objectentity("处罚结果", xzcfEntity.getJg()));
            }
            result.put("list", newList);
        }
        result.put("resultCode", "0");
        result.put("resultMsg", "查询成功");
        return result;
    }
}
